package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

public class _1688BbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://club.1688.com/threadview/[0-9]*.html",
				"http://club.1688.com/threadview/[0-9]*.html[?]forumId=[0-9]*",
				"http://.*.1688.com/thread/[0-9]*/[0-9]*.htm",
				"http://club.1688.com/threadview/[0-9]*.htm?.*"
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);

		title = doc.select("h1.thread-title span").text();
		date = DateUtils.matchDate(doc.select("div.thread-info").text());
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.thread-body"));
		author = doc.select("div.thread-info a.thread-author").text();
		if(author==null||author.trim().equals("")){
			author = doc.select("div.user-name a.nickname").text();
		}
		String text = doc.select("div.thread-info").text();
		clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "浏览:"));
		if(clickNum==null||clickNum==0){
			clickNum=NumberUtils.matchNumber(doc.select("a.thread-views .count").text());
		}
		commnetNum = NumberUtils.matchNumber(StringUtils.substringBetween(text, "评论:", "浏览:"));
		if(commnetNum==null||commnetNum==0){
			commnetNum=NumberUtils.matchNumber(doc.select("a.thread-comment .count").text());
		}
		NewsMeta news = new NewsMeta();
		news.setUrl(url);
		news.setType(4);
		news.setAuthor(author);
		news.setTitle(StringUtils.trimSpace(title));
		news.setCommentNum(commnetNum);
		news.setClickNum(clickNum);
		news.setContent(StringUtils.trimSpace(content));
		news.setDate(date);
		news.setUpdateUrl(url);
		return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	


	public static void main(String[] args) {

		_1688BbsAnalyse a = new _1688BbsAnalyse();
		String url = "http://quan.1688.com/thread/9623/11010348.htm";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		if(a.isDetailPage(url)){
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		}else{
			System.out.println("正则不匹配");
		}


	}

}
